<?php
namespace app\teacher\controller;

use think\Db;
use EasyWeChat\Factory;

class Classes extends Base {

    protected $config = [
        'app_id' => 'wx3cf0f39249eb0exx',
        'secret' => 'f1c242f4f28f735d4687abb469072axx',
        // 下面为可选项
        // 指定 API 调用返回结果的类型：array(default)/collection/object/raw/自定义类名
        'response_type' => 'array',
        'log' => [
            'level' => 'debug',
            'file' => __DIR__.'/wechat.log',
        ],
    ];

    //小程序码
    public function get_qrCode($url){
        $app = Factory::miniProgram($this->config);
        $response = $app->app_code->getQrCode($url);

        // $response 成功时为 EasyWeChat\Kernel\Http\StreamResponse 实例，失败为数组或你指定的 API 返回类型
        if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
            $filename = $response->saveAs('/path/to/directory', 'appcode.png');
        }
        return $filename;
    }

    //创建班级
    public function create(){
        $res['profile_id'] = $this->teacher_id();
        $res['uid'] = Db::name('classes_teacher')->where(['profile_id'=>$this->teacher_id()])->value('admin_id');
        $res['start_time'] = date('Y-m-d');
        $param = input();
        if(isset_full($param,'title')){
            $res['title'] = $param['title'];
        }else{
            return messages(201,'请输入班级名称');
        }
        if(isset_full($param,'comment')){
            $res['comment'] = $param['comment'];
        }else{
            return messages(201,'请输入备注');
        }
        if(isset_full($param,'entry_year')){
            $res['entry_year'] = $param['entry_year'];
        }else{
            return messages(201,'请输入入学年份');
        }
        $res['code'] = mt_rand(100000, 999999);
        $rs = Db::name('classes_class')->insertGetId($res);
        if($rs){
//            $code_img = $this->get_qrCode('/pages/index/index?classes_id='.$rs);
//            Db::name('classes_class')->where(['id'=>$rs])->update(['code_img'=>$code_img]);
            return messages(200, '创建班级成功');
        }else{
            return messages(201, '创建班级失败');
        }
    }

    //获取加班码
    public function get_code(){
        $classes_id = request()->get('classes_id');
        $data = Db::name('classes_class')->where(['id'=>$classes_id])->field('id,code')->find();
        return messages(200, '请求成功', $data);
    }

    //解散班级
    public function del_classes(){
        $classes_id = request()->get('classes_id');
        if($classes_id){
            if(Db::name('classes_class')->where(['id'=>$classes_id])->update(['is_del'=>2])){
                return messages(200, '班级已解散');
            }else{
                return messages(201, '班级解散失败');
            }
        }else{
            return messages(201, '班级解散失败');
        }
    }


	//班级列表
	public function lists(){
		$uid = $this->teacher_id();
		$where['profile_id'] = $uid;
		$type = input('type');
        $where['is_del'] = 0;
		if($type){
			$where['is_end_course'] = $type;
		}else{
			$where['is_end_course'] = 0;
		}
		$lists = Db::name('classes_class')->where($where)->field('id,title,allow_join,code_img,code,comment')->select();
		if($lists){
			foreach ($lists as $key => $value){
				$lists[$key]['student_num'] = Db::name('classes_student')->where(['classes_id'=>$value['id']])->count();
			}
		}
		return messages(200, '请求成功', $lists);
	}

	//班级下的学生列表
	public function student_list(){
		$classes_id = input('classes_id');
		if($classes_id){
			$where['a.classes_id'] = $classes_id;
		}else{
			return json(['code'=>201, 'msg'=>'参数错误']);
		}
        $data['class_name'] = Db::name('classes_class')->where(['id'=>$classes_id])->value('title');
		$lists = Db::name('classes_student')->alias('a')
            ->join('fa_user b','a.uid=b.id')
            ->field('a.id,a.realname,a.student_avatar,a.student_number,b.nickname')
            ->order('a.student_number asc')->where($where)->select();
        $data['lists'] = $lists;
        $data['student_num'] = count($lists);
		return messages(200, '请求成功', $data);
	}

	//禁止不禁止加入班级
	public function class_join_status(){
		$where['profile_id'] = $this->teacher_id();
		$classes_id = input('classes_id');
		if($classes_id){
			$where['id'] = $classes_id;
		}else{
			return json(['code'=>201, 'msg'=>'参数错误']);
		}
		$is_end_course = Db::name('classes_class')->where($where)->field('is_end_course,allow_join')->find();
		if($is_end_course['is_end_course'] == 0){
			if($is_end_course['allow_join'] == 1){
				$res['allow_join'] = 0;
			}else{
				$res['allow_join'] = 1;
			}
			$rs = Db::name('classes_class')->where($where)->update($res);
			if($rs){
				return messages(200, '操作成功');
			}else{
				return messages(201, '操作失败');
			}
		}else{
			return messages(201, '当前班级已结课');
		}
		
	}


	//结课/不结课
	public function class_status(){
		$where['profile_id'] = $this->teacher_id();
		$classes_id = input('classes_id');
		if($classes_id){
			$where['id'] = $classes_id;
		}else{
			return json(['code'=>201, 'msg'=>'参数错误']);
		}
		$is_end_course = Db::name('classes_class')->where($where)->field('is_end_course')->find();
		if($is_end_course['is_end_course'] == 1){
			$res['is_end_course'] = 0;
			$res['allow_join'] = 1;
		}else{
			$res['is_end_course'] = 1;
			$res['allow_join'] = 0;
		}
		$rs = Db::name('classes_class')->where($where)->update($res);
		if($rs){
			return messages(200, '操作成功');
		}else{
			return messages(201, '操作失败');
		}
	}


	//踢出学生
	public function tc_student(){
		$cs_id = input('cs_id');
		if($cs_id){
			$where['id'] = $cs_id;
		}else{
			return json(['code'=>201, 'msg'=>'参数错误']);
		}
		if(Db::name('classes_student')->where($where)->delete()){
			return messages(200, '踢出成功');
		}else{
			return messages(201, '踢出失败');
		}
	}

}